﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MN.Framework.Common;
using MN.Framework.MetaData;
using Telerik.WinControls.UI;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Drawing;

namespace MN.Framework.UI.Win
{

    public class MnPageViewPage<EntityType> : Telerik.WinControls.UI.RadPageViewPage where EntityType : BaseEntity
    {
        #region Peroperty
        Type t = typeof(EntityType);
        MnGridView<EntityType> gridview;
        MnFormEntity<EntityType> formEntity;
        public Telerik.WinControls.UI.RadMenu ToolBar;
        #endregion
        public MnPageViewPage(BaseContext context, int parentWidth, IBaseView<EntityType> winView)
        {
            EntityHeaderAttribute MyAttribute = (EntityHeaderAttribute)Attribute.GetCustomAttribute(t, typeof(EntityHeaderAttribute));
            if (MyAttribute != null)
                this.Text = MyAttribute.Title;

            this.gridview = new MnGridView<EntityType>(context, winView);
            this.formEntity = new MnFormEntity<EntityType>(context, (parentWidth * 25) / 100);
            InitializeComponent(parentWidth);
        }
        public MnPageViewPage(BaseContext context, int parentWidth)
        {
            EntityHeaderAttribute MyAttribute = (EntityHeaderAttribute)Attribute.GetCustomAttribute(t, typeof(EntityHeaderAttribute));
            if (MyAttribute != null)
                this.Text = MyAttribute.Title;

            gridview = new MnGridView<EntityType>(context);
            formEntity = new MnFormEntity<EntityType>(context, (parentWidth * 25) / 100);
            InitializeComponent(parentWidth);
        }

        private void InitializeComponent(int parentWidth)
        {
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            this.SuspendLayout();
            this.ResumeLayout(false);

            #region gridview

            gridview.CurrentRowChanging += gridview_CurrentRowChanging;
            //gridview.ContextMenuStrip.Items.Add("fitttre");// .MenuItems.Add(new MenuItem("filter"));// { Text = "فیلتر کردن اطلاعات", Name = "filter" }            
            #endregion
            #region formEntity
            formEntity.SaveComplated += formEntity_SaveComplated;
            #endregion
            #region Spliter
            RadSplitContainer spliterContiner = new RadSplitContainer();
            spliterContiner.Dock = System.Windows.Forms.DockStyle.Fill;

            SplitPanel panelGrid = new SplitPanel();
            SplitPanel panelMaster = new SplitPanel();
            panelMaster.SizeInfo.SizeMode = Telerik.WinControls.UI.Docking.SplitPanelSizeMode.Absolute;
            panelMaster.SizeInfo.AbsoluteSize = new Size((parentWidth * 30) / 100, 150);

            panelGrid.Controls.Add(gridview);
            panelMaster.Controls.Add(formEntity);

            spliterContiner.SplitPanels.Add(panelMaster);
            spliterContiner.SplitPanels.Add(panelGrid);

            #endregion
            InitToolBar();
            this.Controls.Add(spliterContiner);
        }
        private void InitToolBar()
        {
            this.ToolBar = new Telerik.WinControls.UI.RadMenu();
            var radMenuItemFilter = new Telerik.WinControls.UI.RadMenuItem();
            var radMenuItemRefresh = new Telerik.WinControls.UI.RadMenuItem();

            //((System.ComponentModel.ISupportInitialize)(this.radMenu1)).BeginInit();
            // 
            // radMenu1
            // 
            this.ToolBar.Items.AddRange(new Telerik.WinControls.RadItem[] {
            radMenuItemFilter,
            radMenuItemRefresh});
            //ToolBar.Location = new System.Drawing.Point(0, 0);
            ToolBar.Dock = DockStyle.Bottom;
            ToolBar.Name = "file";
            ToolBar.Size = new System.Drawing.Size(986, 20);
            ToolBar.TabIndex = 3;
            ToolBar.Text = "پرونده";
            ToolBar.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            //ToolBar.Margin = new System.Windows.Forms.Padding(10);
            // 
            // radMenuItem2
            // 
            radMenuItemFilter.AccessibleDescription = "radMenuItemFilter";
            radMenuItemFilter.AccessibleName = "radMenuItemFilter";
            radMenuItemFilter.Name = "radMenuItemFilter";
            radMenuItemFilter.Text = "فیلتر سطرها";
            radMenuItemFilter.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            radMenuItemFilter.Image = Resource.ResourceImages.Filter_Add20;
            radMenuItemFilter.Click += radMenuItemFilter_Click;
            // 
            // radMenuItemRefresh
            // 
            radMenuItemRefresh.AccessibleDescription = "radMenuItemRefresh";
            radMenuItemRefresh.AccessibleName = "radMenuItemRefresh";
            radMenuItemRefresh.Name = "radMenuItemRefresh";
            radMenuItemRefresh.Text = "بارگذاری مجدد";
            radMenuItemRefresh.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            radMenuItemRefresh.Image = Resource.ResourceImages.Refresh20;
            radMenuItemRefresh.Click += radMenuItemRefresh_Click;
            Controls.Add(this.ToolBar);
        }

        void formEntity_SaveComplated(object sender, int e)
        {
            gridview.DataBind();
            if (e > 0)
            {
                gridview.Rows.SingleOrDefault(x => x.Cells["ID"].Value.ToString() == e.ToString()).IsSelected = true;
                gridview.Rows.SingleOrDefault(x => x.Cells["ID"].Value.ToString() == e.ToString()).IsCurrent = true;
            }
        }

        void gridview_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
        {
            var ID = e.NewRow.Cells["ID"].Value;
            if (ID == null)
            {
                foreach (var control in formEntity.Controls)
                {
                    if (control.GetType() == typeof(RadTextBox) ||
                        control.GetType().BaseType.BaseType == typeof(RadMaskedEditBox))
                    {
                        (control as RadControl).Text = string.Empty;
                    }
                }
            }
            else
            {
                formEntity.DataBind(int.Parse(ID.ToString()));
            }
        }
        void radMenuItemFilter_Click(object sender, EventArgs e)
        {
            this.gridview.EnableFiltering = !this.gridview.EnableFiltering;
        }
        void radMenuItemRefresh_Click(object sender, EventArgs e)
        {
            gridview.DataBind();
        }
    }
}
